﻿using System;
using Hjt212Parser;
using log4net;
using TcpServer.Facade;
using TcpServer.Orm;

namespace TcpServer.DataProcessor
{
    class Platform2021Processor : IProcessor
    {
        private readonly ILog log = LogManager.GetLogger(typeof(Platform2021Processor));

        public void Persist()
        {
            throw new NotImplementedException();
        }

        public void Persist(DustEntities context, SocketClientInfo content)
        {
            if (string.IsNullOrWhiteSpace(content.MN)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(content.ReceivedMsg))
                {
                    //根据MN号获取基本信息
                    var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(content.MN);
                    //有效企业信息
                    if (equipment != null)
                    {
                        var datatime = Parser.GetDateTimeByKey("DataTime", content.ReceivedMsg);
                        //从字符串获取污染物代码s
                        var pollutantCodes = Parser.GetPollutantCodes(content.ReceivedMsg);
                        if (datatime.HasValue)
                        {
                            var main = new OneMinuteDataRCMain()
                            {
                                StatCodeID = equipment.StatCodeID,
                                DevCodeID = equipment.DevCodeID,
                                MonitorTime = datatime.Value
                            };
                            foreach (var code in pollutantCodes)
                            {
                                var factor = BaseInfoBufferAndHelper.GetFactorByCode(code);
                                if (factor == null) continue;
                                var detail = new OneMinuteDataRCDetail();
                                detail.MonitorFactorID = factor.ID;
                                //var flag = Parser.GetStringByKeyFromContent(code + "-Flag", content.ReceivedMsg);
                                //if (flag.Equals("N"))
                                //{
                                    detail.Avg = Parser.GetDoubleByKey(code + "-Avg", content.ReceivedMsg);
                                    detail.Min = Parser.GetDoubleByKey(code + "-Min", content.ReceivedMsg);
                                    detail.Max = Parser.GetDoubleByKey(code + "-Max", content.ReceivedMsg);
                                //}
                                main.OneMinuteDataRCDetails.Add(detail);
                            }
                            context.OneMinuteDataRCMains.Add(main);
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        log.Warn( content.MN + " 没有对应基础信息！");
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error( content.ReceivedMsg);

            }
        }
        public void Persist(DataCommunicationEntities context, SocketClientInfo content)
        {
            throw new NotImplementedException();
        }
    }
}
